package com.fbli.controller;

import com.fbli.entity.Result;
import com.fbli.pojo.Knowledge;
import com.fbli.pojo.Products;
import com.fbli.service.ProductsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductsController{
    @Autowired
    private ProductsService productsService;
    @RequestMapping("/selectAll")
    public List<Products> selectAll(){
        return productsService.selectAll();
    }
    @RequestMapping("/selectPage")
    public PageInfo<Products> selectPage(@RequestParam int pageNum,@RequestParam int pageSize){
        return productsService.selectPage(pageNum,pageSize);
    }

    @RequestMapping("/selectById")
    public Products selectById(int productId){
        return productsService.selectById(productId);
    }

    @RequestMapping("/selectCategory")
    public List selectCategory(){
        return productsService.selectCategory();
    }
    @RequestMapping("/selectBrand")
    public List selectBrand(){
        return productsService.selectBrand();
    }
    @RequestMapping("/selectFunction")
    public List selectFunction(){
        return productsService.selectFunction();
    }
    @RequestMapping("/selectCondition")
    public PageInfo<Products> selectCondition(@RequestBody Products products,@RequestParam int pageNum,@RequestParam int pageSize){
        return productsService.selectCondition(products,pageNum,pageSize);
    }

    @RequestMapping("/deleteById")
    public void deleteById(long[] ids){
        productsService.deleteById(ids);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Products products){
        try {
            productsService.update(products);
            return new Result(true,"成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"异常，失败");
        }
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody Products addProduct){
        System.out.println("经过了这里insert");
        try {
            productsService.insert(addProduct);
            return new Result(true,"成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"异常，失败");
        }
    }

}
